package com.example.camerademo;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CommonCamera extends Activity implements OnClickListener {
	ImageView imageView;
	Button button;
	private File currentFile=null;
	private static final int IMAGE_REQUEST_CODE = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_camera);
		initView();
	}
	private void initView() {
		imageView=(ImageView) findViewById(R.id.imageView1);
		button= (Button) findViewById(R.id.btn_startphotp);
		button.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		File dir=new File(Environment.getExternalStorageDirectory(), "photos");
		if(!dir.exists()){
			dir.mkdir();
		}
		currentFile=new File(dir, System.currentTimeMillis()+".jpg");
		if(!currentFile.exists()){
			try {
				currentFile.createNewFile();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(currentFile));
		startActivityForResult(intent, IMAGE_REQUEST_CODE);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==IMAGE_REQUEST_CODE){
			if(resultCode==RESULT_OK){
				imageView.setImageURI(Uri.fromFile(currentFile));
			}else{
				currentFile.delete();
			}
		}
	}
}
